﻿using System;
using System.Web.UI;
using System.Xml;

namespace ud_GoldWeatherEtc
{
    public partial class Weather : UserControl
    {
        public string ImageSiteRoot { get; set; }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);
            Load += WeatherLoad;
            ddlWeather.SelectedIndexChanged += ddlWeather_SelectedIndexChanged;
        }

        void ddlWeather_SelectedIndexChanged(object sender, EventArgs e)
        {
            divWeather.InnerHtml = GetWeather(ddlWeather.SelectedValue);
        }

        void WeatherLoad(object sender, EventArgs e)
        {
            Page.EnableViewState = true;
            divWeather.InnerHtml = GetWeather("HCM");
        }

        public string GetWeather(string city)
        {
            string strWrite = "";
            XmlTextReader reader = null;
            try
            {
                reader = new XmlTextReader("http://vnexpress.net/ListFile/Weather/" + city + ".xml");
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(reader);

                XmlNodeList nodelist = xmldoc.SelectNodes("Item");
                if (nodelist != null)
                {
// ReSharper disable PossibleNullReferenceException
                    XmlNode nodedetail = nodelist.Item(0).SelectSingleNode("AdImg");
// ReSharper restore PossibleNullReferenceException
                    if (nodedetail != null)
                    {
                        string adImg = nodedetail.InnerText;
// ReSharper disable PossibleNullReferenceException
                        nodedetail = nodelist.Item(0).SelectSingleNode("AdImg1");
// ReSharper restore PossibleNullReferenceException
                        if (nodedetail != null)
                        {
                            string adImg1 = nodedetail.InnerText;
// ReSharper disable PossibleNullReferenceException
                            nodedetail = nodelist.Item(0).SelectSingleNode("AdImg2");
// ReSharper restore PossibleNullReferenceException
                            if (nodedetail != null)
                            {
                                string adImg2 = nodedetail.InnerText;
// ReSharper disable PossibleNullReferenceException
                                nodedetail = nodelist.Item(0).SelectSingleNode("Weather");
// ReSharper restore PossibleNullReferenceException
                                if (nodedetail != null)
                                {
                                    string weather = nodedetail.InnerText;
                                    string path = ImageSiteRoot + "/Data/Images/Weather/";
                                    strWrite += "<img src='" + path + adImg + "' border='0' height='18' />&nbsp;";
                                    strWrite += "<img src='" + path + adImg1 + "' border='0' height='18' />";
                                    strWrite += "<img src='" + path + adImg2 + "' border='0' height='18' />";
                                    strWrite += "<img src='" + path + "c.gif' height='18' />&nbsp;";
                                    strWrite += weather.Remove(weather.IndexOf(".")) + "&nbsp;";
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                strWrite = ex.Message;
            }
            finally
            {
                if (reader != null) reader.Close();
            }
            return strWrite;
        }
    }
}